package pro.shushi.pamirs.hr.core.action;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.core.common.FetchUtil;
import pro.shushi.pamirs.hr.api.model.org.Employee;
import pro.shushi.pamirs.hr.api.service.EmployeeService;
import pro.shushi.pamirs.hr.api.tmodel.EmployeeBatchReq;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.common.util.ListUtils;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.List;

@Model.model(EmployeeBatchReq.MODEL_MODEL)
@Component
public class EmployeeBatchReqAction {

    @Autowired
    private EmployeeService employeeService;

    @Function(openLevel = FunctionOpenEnum.API)
    @Function.Advanced(type = FunctionTypeEnum.QUERY)
    public EmployeeBatchReq construct(EmployeeBatchReq employeeBatchReq, List<Employee> employees) {
        EmployeeBatchReq result = new EmployeeBatchReq();
        List<Long> ids = ListUtils.transform(employees, Employee::getId);
        if (CollectionUtils.isNotEmpty(ids)) {
            employees = FetchUtil.fetchListByIds(Employee.class, ids);
        }
        result.setEmployees(employees);
        return result;
    }

    /**
     * Action的实现抽取到employeeService中
     * @param data
     * @return
     */
    @Action(displayName = "确定", bindingType = ViewTypeEnum.FORM)
    public EmployeeBatchReq conform(EmployeeBatchReq data) {
        employeeService.batchUpdateEmpLabel(data);
        return data;
    }

}